package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.query.ChartQueryObject;
import cn.wolfcode.crm.service.IChartService;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/chart")
public class ChartController {
	
	@Autowired
	private IChartService chartService;


	@RequestMapping("list")
	public String list(Model model, @ModelAttribute("qo")ChartQueryObject qo){

		model.addAttribute("result", chartService.query(qo));
		model.addAttribute("groupTypes",ChartQueryObject.GROUP_TYPE_MAP);
		return "/chart/customerChart";
	}

	@RequestMapping("list_bar")
	public String list_bar(Model model, @ModelAttribute("qo")ChartQueryObject qo){

		qo.setPageSize(0);
		List<HashMap> list = chartService.query(qo).getList();
		List groupTypes= new ArrayList();
		List customerNums= new ArrayList();
		for (HashMap hashMap : list) {
			groupTypes.add(hashMap.get("groupType"));
			customerNums.add(hashMap.get("customerNum"));
		}
		model.addAttribute("groupTypeName",ChartQueryObject.GROUP_TYPE_MAP.get(qo.getGroupType()));
		model.addAttribute("groupTypes", JSON.toJSONString(groupTypes));
		model.addAttribute("customerNums",JSON.toJSONString(customerNums));
		return "/chart/customerChart_bar";
	}

	@RequestMapping("list_bing")
	public String list_bing(Model model, @ModelAttribute("qo")ChartQueryObject qo){

		qo.setPageSize(0);
		List<HashMap> list = chartService.query(qo).getList();
		List groupTypes= new ArrayList();
		List customerNums= new ArrayList();
		for (HashMap hashMap : list) {
			groupTypes.add(hashMap.get("groupType"));
			Map map = new HashMap();
			map.put("name",hashMap.get("groupType"));
			map.put("value",hashMap.get("customerNum"));
			customerNums.add(map);

		}
		model.addAttribute("groupTypeName",ChartQueryObject.GROUP_TYPE_MAP.get(qo.getGroupType()));
		model.addAttribute("groupTypes", JSON.toJSONString(groupTypes));
		model.addAttribute("customerNums",JSON.toJSONString(customerNums));
		return "/chart/customerChart_bing";
	}
}
